In [4]:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
pd.options.display.max_columns=1000
In [5]:
df_train = pd.read_csv("/data/MNIST/mnist_train.csv", header = None)
df_train.head()
Out[5]:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
0
5
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3
18
18
18
126
136
175
26
166
255
247
127
0
0
0
0
0
0
0
0
0
0
0
0
30
36
94
154
170
253
253
253
253
253
225
172
253
242
195
64
0
0
0
0
0
0
0
0
0
0
0
49
238
253
253
253
253
253
253
253
253
251
93
82
82
56
39
0
0
0
0
0
0
0
0
0
0
0
0
18
219
253
253
253
253
253
198
182
247
241
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
80
156
107
253
253
205
11
0
43
154
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
14
1
154
253
90
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
139
253
190
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
11
190
253
70
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
35
241
225
160
108
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
81
240
253
253
119
25
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
45
186
253
253
150
27
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
16
93
252
253
187
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
249
253
249
64
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
46
130
183
253
253
207
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
39
148
229
253
253
253
250
182
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
24
114
221
253
253
253
253
201
78
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
23
66
213
253
253
253
253
198
81
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
18
171
219
253
253
253
253
195
80
9
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
55
172
226
253
253
253
253
244
133
11
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
136
253
253
253
212
135
132
16
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
51
159
253
159
50
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
48
238
252
252
252
237
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
54
227
253
252
239
233
252
57
6
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
10
60
224
252
253
252
202
84
252
253
122
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
163
252
252
252
253
252
252
96
189
253
167
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
51
238
253
253
190
114
253
228
47
79
255
168
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
48
238
252
252
179
12
75
121
21
0
0
253
243
50
0
0
0
0
0
0
0
0
0
0
0
0
0
38
165
253
233
208
84
0
0
0
0
0
0
253
252
165
0
0
0
0
0
0
0
0
0
0
0
0
7
178
252
240
71
19
28
0
0
0
0
0
0
253
252
195
0
0
0
0
0
0
0
0
0
0
0
0
57
252
252
63
0
0
0
0
0
0
0
0
0
253
252
195
0
0
0
0
0
0
0
0
0
0
0
0
198
253
190
0
0
0
0
0
0
0
0
0
0
255
253
196
0
0
0
0
0
0
0
0
0
0
0
76
246
252
112
0
0
0
0
0
0
0
0
0
0
253
252
148
0
0
0
0
0
0
0
0
0
0
0
85
252
230
25
0
0
0
0
0
0
0
0
7
135
253
186
12
0
0
0
0
0
0
0
0
0
0
0
85
252
223
0
0
0
0
0
0
0
0
7
131
252
225
71
0
0
0
0
0
0
0
0
0
0
0
0
85
252
145
0
0
0
0
0
0
0
48
165
252
173
0
0
0
0
0
0
0
0
0
0
0
0
0
0
86
253
225
0
0
0
0
0
0
114
238
253
162
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
85
252
249
146
48
29
85
178
225
253
223
167
56
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
85
252
252
252
229
215
252
252
252
196
130
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
28
199
252
252
253
252
252
233
145
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
25
128
252
253
252
141
37
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
4
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
67
232
39
0
0
0
0
0
0
0
0
0
62
81
0
0
0
0
0
0
0
0
0
0
0
0
0
0
120
180
39
0
0
0
0
0
0
0
0
0
126
163
0
0
0
0
0
0
0
0
0
0
0
0
0
2
153
210
40
0
0
0
0
0
0
0
0
0
220
163
0
0
0
0
0
0
0
0
0
0
0
0
0
27
254
162
0
0
0
0
0
0
0
0
0
0
222
163
0
0
0
0
0
0
0
0
0
0
0
0
0
183
254
125
0
0
0
0
0
0
0
0
0
46
245
163
0
0
0
0
0
0
0
0
0
0
0
0
0
198
254
56
0
0
0
0
0
0
0
0
0
120
254
163
0
0
0
0
0
0
0
0
0
0
0
0
23
231
254
29
0
0
0
0
0
0
0
0
0
159
254
120
0
0
0
0
0
0
0
0
0
0
0
0
163
254
216
16
0
0
0
0
0
0
0
0
0
159
254
67
0
0
0
0
0
0
0
0
0
14
86
178
248
254
91
0
0
0
0
0
0
0
0
0
0
159
254
85
0
0
0
47
49
116
144
150
241
243
234
179
241
252
40
0
0
0
0
0
0
0
0
0
0
150
253
237
207
207
207
253
254
250
240
198
143
91
28
5
233
250
0
0
0
0
0
0
0
0
0
0
0
0
119
177
177
177
177
177
98
56
0
0
0
0
0
102
254
220
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
169
254
137
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
169
254
57
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
169
254
57
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
169
255
94
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
169
254
96
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
169
254
153
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0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
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
55
148
210
253
253
113
87
148
55
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
87
232
252
253
189
210
252
252
253
168
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
4
57
242
252
190
65
5
12
182
252
253
116
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
96
252
252
183
14
0
0
92
252
252
225
21
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
132
253
252
146
14
0
0
0
215
252
252
79
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
126
253
247
176
9
0
0
8
78
245
253
129
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
16
232
252
176
0
0
0
36
201
252
252
169
11
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
22
252
252
30
22
119
197
241
253
252
251
77
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
16
231
252
253
252
252
252
226
227
252
231
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
55
235
253
217
138
42
24
192
252
143
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
62
255
253
109
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
71
253
252
21
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
253
252
21
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
71
253
252
21
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
106
253
252
21
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
45
255
253
21
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
218
252
56
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
96
252
189
42
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
14
184
252
170
11
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
14
147
252
42
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
In [6]:
df_train.shape
Out[6]:
(60000, 785)
In [7]:
28 * 28
Out[7]:
784
In [9]:
df_train.iloc[0, 1:].shape
Out[9]:
(784,)
In [12]:
df_train.iloc[0, 1:].values.reshape((28,28)).shape
Out[12]:
(28, 28)
In [15]:
plt.imshow(df_train.iloc[0, 1:].values.reshape((28,28)), cmap="gray")
plt.title("t: %d" % (df_train.iloc[0, 0]))
Out[15]:
Text(0.5, 1.0, 't: 5')
In [14]:
df_train.iloc[0, 0]
Out[14]:
5
In [19]:
fig, _ = plt.subplots(10, 10, figsize = (20, 20))
for i, ax in enumerate(fig.axes):
ax.imshow(df_train.iloc[i, 1:].values.reshape((28,28)), cmap="gray")
ax.set_title("t: %d" % (df_train.iloc[i, 0]))
plt.tight_layout()
In [21]:
X_train = df_train.iloc[:, 1:].values
y_train = df_train.iloc[:, 0].values
In [23]:
a = X_train.flatten()
a.min(),a.max()
Out[23]:
(0, 255)
In [25]:
plt.hist(a, bins = 50);
In [ ]:
In [28]:
len(a[a>0])/len(a)
Out[28]:
0.19120229591836735
In [30]:
X_train[0].shape
Out[30]:
(784,)
In [32]:
X_train[0].mean(), X_train[0].std()
Out[32]:
(35.108418367346935, 79.64882892760731)
In [38]:
X_train = df_train.iloc[:, 1:].values/255.0
y_train = df_train.iloc[:, 0].values
In [34]:
a = X_train.flatten()
a.min(),a.max()
Out[34]:
(0.0, 0.99609375)
In [35]:
plt.hist(a, bins = 50);
In [86]:
df_test = pd.read_csv("/data/MNIST/mnist_test.csv", header = None)
df_test.head()
Out[86]:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
0
7
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
84
185
159
151
60
36
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
222
254
254
254
254
241
198
198
198
198
198
198
198
198
170
52
0
0
0
0
0
0
0
0
0
0
0
0
67
114
72
114
163
227
254
225
254
254
254
250
229
254
254
140
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
17
66
14
67
67
67
59
21
236
254
106
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
83
253
209
18
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
22
233
255
83
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
129
254
238
44
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
59
249
254
62
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
133
254
187
5
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
9
205
248
58
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
126
254
182
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
75
251
240
57
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
19
221
254
166
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3
203
254
219
35
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
38
254
254
77
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
31
224
254
115
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
133
254
254
52
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
61
242
254
254
52
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
121
254
254
219
40
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
121
254
207
18
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
116
125
171
255
255
150
93
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
169
253
253
253
253
253
253
218
30
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
169
253
253
253
213
142
176
253
253
122
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
52
250
253
210
32
12
0
6
206
253
140
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
77
251
210
25
0
0
0
122
248
253
65
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
31
18
0
0
0
0
209
253
253
65
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
117
247
253
198
10
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
76
247
253
231
63
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
128
253
253
144
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
176
246
253
159
12
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
25
234
253
233
35
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
198
253
253
141
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
78
248
253
189
12
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
19
200
253
253
141
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
134
253
253
173
12
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
248
253
253
25
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
248
253
253
43
20
20
20
20
5
0
5
20
20
37
150
150
150
147
10
0
0
0
0
0
0
0
0
0
248
253
253
253
253
253
253
253
168
143
166
253
253
253
253
253
253
253
123
0
0
0
0
0
0
0
0
0
174
253
253
253
253
253
253
253
253
253
253
253
249
247
247
169
117
117
57
0
0
0
0
0
0
0
0
0
0
118
123
123
123
166
253
253
253
155
123
123
41
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
38
254
109
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
87
252
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0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
11
150
253
202
31
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
37
251
251
253
107
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
21
197
251
251
253
107
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
110
190
251
251
251
253
169
109
62
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
253
251
251
251
251
253
251
251
220
51
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
182
255
253
253
253
253
234
222
253
253
253
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
63
221
253
251
251
251
147
77
62
128
251
251
105
0
0
0
0
0
0
0
0
0
0
0
0
0
0
32
231
251
253
251
220
137
10
0
0
31
230
251
243
113
5
0
0
0
0
0
0
0
0
0
0
0
0
37
251
251
253
188
20
0
0
0
0
0
109
251
253
251
35
0
0
0
0
0
0
0
0
0
0
0
0
37
251
251
201
30
0
0
0
0
0
0
31
200
253
251
35
0
0
0
0
0
0
0
0
0
0
0
0
37
253
253
0
0
0
0
0
0
0
0
32
202
255
253
164
0
0
0
0
0
0
0
0
0
0
0
0
140
251
251
0
0
0
0
0
0
0
0
109
251
253
251
35
0
0
0
0
0
0
0
0
0
0
0
0
217
251
251
0
0
0
0
0
0
21
63
231
251
253
230
30
0
0
0
0
0
0
0
0
0
0
0
0
217
251
251
0
0
0
0
0
0
144
251
251
251
221
61
0
0
0
0
0
0
0
0
0
0
0
0
0
217
251
251
0
0
0
0
0
182
221
251
251
251
180
0
0
0
0
0
0
0
0
0
0
0
0
0
0
218
253
253
73
73
228
253
253
255
253
253
253
253
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
113
251
251
253
251
251
251
251
253
251
251
251
147
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
31
230
251
253
251
251
251
251
253
230
189
35
10
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
62
142
253
251
251
251
251
253
107
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
72
174
251
173
71
72
30
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
4
4
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
50
224
0
0
0
0
0
0
0
70
29
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
121
231
0
0
0
0
0
0
0
148
168
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
4
195
231
0
0
0
0
0
0
0
96
210
11
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
69
252
134
0
0
0
0
0
0
0
114
252
21
0
0
0
0
0
0
0
0
0
0
0
0
0
0
45
236
217
12
0
0
0
0
0
0
0
192
252
21
0
0
0
0
0
0
0
0
0
0
0
0
0
0
168
247
53
0
0
0
0
0
0
0
18
255
253
21
0
0
0
0
0
0
0
0
0
0
0
0
0
84
242
211
0
0
0
0
0
0
0
0
141
253
189
5
0
0
0
0
0
0
0
0
0
0
0
0
0
169
252
106
0
0
0
0
0
0
0
32
232
250
66
0
0
0
0
0
0
0
0
0
0
0
0
0
15
225
252
0
0
0
0
0
0
0
0
134
252
211
0
0
0
0
0
0
0
0
0
0
0
0
0
0
22
252
164
0
0
0
0
0
0
0
0
169
252
167
0
0
0
0
0
0
0
0
0
0
0
0
0
0
9
204
209
18
0
0
0
0
0
0
22
253
253
107
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
169
252
199
85
85
85
85
129
164
195
252
252
106
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
41
170
245
252
252
252
252
232
231
251
252
252
9
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
49
84
84
84
84
0
0
161
252
252
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
127
252
252
45
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
128
253
253
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
127
252
252
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
135
252
244
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
232
236
111
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
179
66
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
In [87]:
X_test = df_test.iloc[:, 1:].values/255.0
y_test = df_test.iloc[:, 0].values
In [40]:
from sklearn import *
In [88]:
est = linear_model.SGDClassifier(n_jobs=8, tol=1e-5, eta0 = 0.15,
learning_rate = "invscaling",
alpha = 0.01, max_iter= 100)
est.fit(X_train, y_train)
print("accuracy", est.score(X_test, y_test), "iterations:", est.n_iter_)
accuracy 0.9082 iterations: 61
In [44]:
est.coef_.shape
Out[44]:
(10, 784)
In [89]:
X_test.shape
Out[89]:
(10000, 784)
In [47]:
pd.Series(y_train).nunique()
Out[47]:
10
In [50]:
pd.Series(y_train).value_counts().sort_index().plot.bar()
Out[50]:
<matplotlib.axes._subplots.AxesSubplot at 0x12298a650>
In [52]:
est.coef_[0].shape
Out[52]:
(784,)
In [55]:
est.coef_[0].min(), est.coef_[0].max()
Out[55]:
(-0.20998576864335874, 0.11819429479848767)
In [57]:
plt.imshow(est.coef_[0].reshape((28, 28)), cmap="gray")
Out[57]:
<matplotlib.image.AxesImage at 0x122c12510>
In [60]:
import tensorflow as tf
import keras
In [90]:
Y_train = keras.utils.to_categorical(y_train)
Y_test = keras.utils.to_categorical(y_test)
Y_train[:10,:]
Out[90]:
array([[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]], dtype=float32)
In [91]:
y_train[:10]
Out[91]:
array([5, 0, 4, 1, 9, 2, 1, 3, 1, 4])
In [92]:
from time import time
tensor_board = keras.callbacks.TensorBoard("/tmp/tf/%d" % time())
adam = keras.optimizers.Adam(lr = 0.005)
model = keras.Sequential([
keras.layers.InputLayer(input_shape=(784, )),
keras.layers.Dense(400, activation="relu"),
keras.layers.Dense(100, activation="relu"),
keras.layers.Dense(10, activation="softmax")
])
model.summary()
model.compile(optimizer = adam, loss = keras.losses.categorical_crossentropy, metrics = ["acc"])
model.fit(X_train, Y_train, batch_size = 256, epochs=10
, validation_data = (X_test, Y_test), callbacks = [tensor_board])
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_14 (Dense) (None, 400) 314000
_________________________________________________________________
dense_15 (Dense) (None, 100) 40100
_________________________________________________________________
dense_16 (Dense) (None, 10) 1010
=================================================================
Total params: 355,110
Trainable params: 355,110
Non-trainable params: 0
_________________________________________________________________
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 3s 50us/step - loss: 0.2288 - acc: 0.9287 - val_loss: 0.1036 - val_acc: 0.9679
Epoch 2/10
60000/60000 [==============================] - 3s 47us/step - loss: 0.0891 - acc: 0.9718 - val_loss: 0.1086 - val_acc: 0.9674
Epoch 3/10
60000/60000 [==============================] - 3s 47us/step - loss: 0.0614 - acc: 0.9799 - val_loss: 0.0856 - val_acc: 0.9744
Epoch 4/10
60000/60000 [==============================] - 3s 46us/step - loss: 0.0483 - acc: 0.9846 - val_loss: 0.0806 - val_acc: 0.9767
Epoch 5/10
60000/60000 [==============================] - 3s 46us/step - loss: 0.0421 - acc: 0.9857 - val_loss: 0.0916 - val_acc: 0.9743
Epoch 6/10
60000/60000 [==============================] - 3s 46us/step - loss: 0.0362 - acc: 0.9879 - val_loss: 0.0836 - val_acc: 0.9758
Epoch 7/10
60000/60000 [==============================] - 3s 46us/step - loss: 0.0291 - acc: 0.9907 - val_loss: 0.0826 - val_acc: 0.9773
Epoch 8/10
60000/60000 [==============================] - 3s 46us/step - loss: 0.0253 - acc: 0.9922 - val_loss: 0.0780 - val_acc: 0.9801
Epoch 9/10
60000/60000 [==============================] - 3s 48us/step - loss: 0.0209 - acc: 0.9933 - val_loss: 0.1002 - val_acc: 0.9742
Epoch 10/10
60000/60000 [==============================] - 3s 47us/step - loss: 0.0262 - acc: 0.9917 - val_loss: 0.0968 - val_acc: 0.9774
Out[92]:
<keras.callbacks.History at 0x16c004c10>
In [93]:
y_test_pred = model.predict_classes(X_test)
In [94]:
y_test_pred.shape
Out[94]:
(10000,)
In [81]:
from sklearn import *
In [96]:
print(metrics.classification_report(y_test, y_test_pred ))
precision recall f1-score support
0 0.99 0.99 0.99 980
1 1.00 0.99 0.99 1135
2 0.99 0.97 0.98 1032
3 0.97 0.98 0.98 1010
4 0.98 0.98 0.98 982
5 0.99 0.97 0.98 892
6 0.97 0.99 0.98 958
7 0.98 0.96 0.97 1028
8 0.96 0.98 0.97 974
9 0.96 0.97 0.96 1009
accuracy 0.98 10000
macro avg 0.98 0.98 0.98 10000
weighted avg 0.98 0.98 0.98 10000
In [97]:
In [102]:
errors = y_test != y_test_pred
fig, _ = plt.subplots(10, 10, figsize = (20, 20))
for i, ax in enumerate(fig.axes):
ax.imshow(X_test[errors][i, :].reshape((28,28)), cmap="gray")
ax.set_title("t: %d, p: %d" % (y_test[errors][i], y_test_pred[errors][i]))
plt.tight_layout()
In [103]:
from skimage.io import imread
In [104]:
elephant = imread("/Users/abasar/Downloads/elephant.jpg")
In [106]:
elephant.shape
Out[106]:
(1363, 2048, 3)
In [107]:
plt.imshow(elephant)
Out[107]:
<matplotlib.image.AxesImage at 0x124819a50>
In [118]:
import cv2
In [120]:
elephant_gray = cv2.cvtColor(elephant, cv2.COLOR_BGR2GRAY)
In [121]:
plt.imshow(elephant_gray, cmap="gray")
Out[121]:
<matplotlib.image.AxesImage at 0x123df1b50>
In [122]:
elephant_gray.shape
Out[122]:
(1363, 2048)
In [117]:
plt.imshow(elephant[:,:,2], cmap="gray")
Out[117]:
<matplotlib.image.AxesImage at 0x123466ed0>
In [113]:
pd.Series(elephant[:,:,2].flatten()).plot.hist(bins = 50)
Out[113]:
<matplotlib.axes._subplots.AxesSubplot at 0x123b38d10>
In [114]:
pd.Series(elephant.flatten()).plot.hist(bins = 50)
Out[114]:
<matplotlib.axes._subplots.AxesSubplot at 0x123a20fd0>
In [115]:
pd.Series(elephant.flatten()).describe()
Out[115]:
count 8.374272e+06
mean 1.287571e+02
std 8.176032e+01
min 0.000000e+00
25% 5.400000e+01
50% 1.150000e+02
75% 2.160000e+02
max 2.550000e+02
dtype: float64
In [124]:
elepant_png = imread("/Users/abasar/Downloads/elephants_PNG18793.png")
In [125]:
elepant_png.shape
Out[125]:
(778, 1097, 4)
In [128]:
plt.hist(elepant_png[:,:,3].flatten(), bins = 50)
Out[128]:
(array([4.50264e+05, 7.53000e+02, 5.03000e+02, 3.57000e+02, 3.54000e+02,
2.51000e+02, 2.33000e+02, 1.79000e+02, 1.94000e+02, 1.74000e+02,
2.18000e+02, 1.82000e+02, 1.75000e+02, 1.67000e+02, 1.36000e+02,
1.50000e+02, 1.29000e+02, 1.44000e+02, 1.27000e+02, 1.52000e+02,
1.69000e+02, 1.33000e+02, 1.24000e+02, 1.40000e+02, 1.27000e+02,
1.38000e+02, 1.30000e+02, 1.36000e+02, 1.41000e+02, 1.36000e+02,
1.64000e+02, 1.63000e+02, 1.47000e+02, 1.61000e+02, 1.70000e+02,
1.71000e+02, 1.79000e+02, 2.10000e+02, 1.88000e+02, 1.98000e+02,
2.57000e+02, 1.92000e+02, 2.71000e+02, 2.77000e+02, 2.87000e+02,
3.42000e+02, 4.03000e+02, 4.97000e+02, 6.83000e+02, 3.92290e+05]),
array([ 0. , 5.1, 10.2, 15.3, 20.4, 25.5, 30.6, 35.7, 40.8,
45.9, 51. , 56.1, 61.2, 66.3, 71.4, 76.5, 81.6, 86.7,
91.8, 96.9, 102. , 107.1, 112.2, 117.3, 122.4, 127.5, 132.6,
137.7, 142.8, 147.9, 153. , 158.1, 163.2, 168.3, 173.4, 178.5,
183.6, 188.7, 193.8, 198.9, 204. , 209.1, 214.2, 219.3, 224.4,
229.5, 234.6, 239.7, 244.8, 249.9, 255. ]),
<a list of 50 Patch objects>)
In [129]:
pd.Series(elepant_png[:,:,3].flatten()).describe()
Out[129]:
count 853466.000000
mean 118.869841
std 126.745542
min 0.000000
25% 0.000000
50% 0.000000
75% 255.000000
max 255.000000
dtype: float64
In [130]:
pd.Series(elepant_png[:,:,3].flatten()).value_counts()
Out[130]:
0 447469
255 390764
1 1330
2 573
254 562
...
112 19
124 18
79 18
120 18
133 17
Length: 256, dtype: int64
In [133]:
X_train_3d = X_train.reshape((60000, 28, 28, 1))
X_test_3d = X_test.reshape((-1, 28, 28, 1))
X_train_3d.shape, X_test_3d.shape
Out[133]:
((60000, 28, 28, 1), (10000, 28, 28, 1))
In [134]:
from time import time
tensor_board = keras.callbacks.TensorBoard("/tmp/tf/cnn-%d" % time())
adam = keras.optimizers.Adam(lr = 0.005)
model = keras.Sequential([
keras.layers.InputLayer(input_shape=(28, 28, 1)),
keras.layers.Conv2D(32, kernel_size=(5, 5), padding="SAME", activation="relu"),
keras.layers.MaxPool2D(pool_size=(2,2), padding="SAME"),
keras.layers.Conv2D(64, kernel_size=(5, 5), padding="SAME", activation="relu"),
keras.layers.MaxPool2D(pool_size=(2,2), padding="SAME"),
keras.layers.Flatten(),
keras.layers.Dropout(rate=0.5),
keras.layers.Dense(400, activation="relu"),
keras.layers.Dense(100, activation="relu"),
keras.layers.Dense(10, activation="softmax")
])
model.summary()
model.compile(optimizer = adam, loss = keras.losses.categorical_crossentropy, metrics = ["acc"])
model.fit(X_train_3d, Y_train, batch_size = 256, epochs=10
, validation_data = (X_test_3d, Y_test), callbacks = [tensor_board])
WARNING:tensorflow:From /anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
WARNING:tensorflow:From /anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 28, 28, 32) 832
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 14, 14, 64) 51264
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 7, 7, 64) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 3136) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 3136) 0
_________________________________________________________________
dense_17 (Dense) (None, 400) 1254800
_________________________________________________________________
dense_18 (Dense) (None, 100) 40100
_________________________________________________________________
dense_19 (Dense) (None, 10) 1010
=================================================================
Total params: 1,348,006
Trainable params: 1,348,006
Non-trainable params: 0
_________________________________________________________________
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 72s 1ms/step - loss: 0.2360 - acc: 0.9230 - val_loss: 0.0436 - val_acc: 0.9868
Epoch 2/10
60000/60000 [==============================] - 108s 2ms/step - loss: 0.0720 - acc: 0.9775 - val_loss: 0.0306 - val_acc: 0.9892
Epoch 3/10
60000/60000 [==============================] - 86s 1ms/step - loss: 0.0553 - acc: 0.9831 - val_loss: 0.0285 - val_acc: 0.9903
Epoch 4/10
60000/60000 [==============================] - 89s 1ms/step - loss: 0.0455 - acc: 0.9856 - val_loss: 0.0336 - val_acc: 0.9891
Epoch 5/10
60000/60000 [==============================] - 92s 2ms/step - loss: 0.0459 - acc: 0.9860 - val_loss: 0.0278 - val_acc: 0.9918
Epoch 6/10
60000/60000 [==============================] - 94s 2ms/step - loss: 0.0410 - acc: 0.9874 - val_loss: 0.0285 - val_acc: 0.9909
Epoch 7/10
60000/60000 [==============================] - 93s 2ms/step - loss: 0.0358 - acc: 0.9892 - val_loss: 0.0313 - val_acc: 0.9903
Epoch 8/10
60000/60000 [==============================] - 94s 2ms/step - loss: 0.0364 - acc: 0.9889 - val_loss: 0.0278 - val_acc: 0.9916
Epoch 9/10
60000/60000 [==============================] - 93s 2ms/step - loss: 0.0325 - acc: 0.9902 - val_loss: 0.0294 - val_acc: 0.9913
Epoch 10/10
60000/60000 [==============================] - 93s 2ms/step - loss: 0.0310 - acc: 0.9906 - val_loss: 0.0286 - val_acc: 0.9913
Out[134]:
<keras.callbacks.History at 0x1d2641850>
In [137]:
y_test_pred = model.predict_classes(X_test_3d)
errors = y_test != y_test_pred
fig, _ = plt.subplots(10, 8, figsize = (20, 20))
for i, ax in enumerate(fig.axes):
if(i < len(errors)):
ax.imshow(X_test[errors][i, :].reshape((28,28)), cmap="gray")
ax.set_title("t: %d, p: %d" % (y_test[errors][i], y_test_pred[errors][i]))
plt.tight_layout()
In [ ]:
Content source: abulbasar/machine-learning
Similar notebooks: